﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Timers;
using System.Windows;
using TicTacToe.Core;

namespace _3DTicTacToe.UI
{
    internal partial class TurnListWindow : Window
    {
        private readonly Timer _timer;
        private readonly Stack<Turn> _turns;

        public TurnListWindow(IEnumerable<Turn> turns)
        {
            InitializeComponent();

            Closing += TurnListWindow_Closing;

            _turns = new Stack<Turn>();
            _timer = new Timer(25);
            _timer.Elapsed += TimerElapsed;

            foreach (var t in turns)
                TurnHandler(null, new TurnEventArgs(t));

            _timer.Start();
        }

        void TurnListWindow_Closing(object sender, CancelEventArgs e)
        {
            _timer.Stop();
            _timer.Dispose();
        }

        void SetElapsedTime(string time)
        {
            elapsedTimeSBItem.Content = time;
        }

        void SetLastTurn(string time)
        {
            lastTurnSBItem.Content = time;
        }

        static string Hours(TimeSpan span)
        {
            var hours = span.Hours.ToString();
            hours = hours.Length == 1 ? "0" + hours : hours;
            return hours;
        }

        static string Minutes(TimeSpan span)
        {
            var minutes = span.Minutes.ToString();
            minutes = minutes.Length == 1 ? "0" + minutes : minutes;
            return minutes;
        }

        static string Seconds(TimeSpan span)
        {
            var seconds = span.Seconds.ToString();
            seconds = seconds.Length == 1 ? "0" + seconds : seconds;
            return seconds;
        }

        void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            var elapsed = "-";
            var last = "-";

            if (_turns.Count > 0)
            {
                var span = (DateTime.Now - _turns.Last().Timestamp);
                var span2 = (DateTime.Now - _turns.Peek().Timestamp);

                var hrs = Hours(span);
                var min = Minutes(span);
                var sec = Seconds(span);

                var min2 = Minutes(span2);
                var sec2 = Seconds(span2);

                elapsed = string.Format("{0}:{1}:{2}", hrs, min, sec);
                last = string.Format("{0}:{1}", min2, sec2);
            }

            Dispatcher.Invoke(new Action<string>(SetElapsedTime), elapsed);
            Dispatcher.Invoke(new Action<string>(SetLastTurn), last);
        }

        public void TurnHandler(object sender, TurnEventArgs e)
        {
            Action<Turn> a = t =>
                                {
                                    var formatted = "00:00";

                                    if (_turns.Count > 0)
                                    {
                                        var span = t.Timestamp - _turns.Peek().Timestamp;

                                        var min = Minutes(span);
                                        var sec = Seconds(span);

                                        formatted = string.Format("+{0}:{1}", min, sec);
                                    }

                                    t.FormattedTimestamp = formatted;

                                    _turns.Push(t);
                                    turnListBox.Items.Add(t);

                                    turnListBox.ScrollIntoView(t);

                                    countSBItem.Content = _turns.Count;
                                };
            Dispatcher.Invoke(a, e.Turn);
        }
    }
}